
import { Message } from 'element-ui'

export function showMessage(msg, type) {
  let t = 'info'
  if (['info', 'success', 'warning', 'error'].includes(type)) {
    t = type
  }
  Message({
    dangerouslyUseHTMLString: true,
    showClose: true,
    message: msg,
    type: t,
    duration: 5000
  })
}
export function showMessageSuccess(msg) {
  showMessage(msg, 'success')
}

export function showMessageError(msg) {
  showMessage(msg, 'error')
}

export function showMessageInfo(msg) {
  showMessage(msg, 'info')
}

export function showMessageWarning(msg) {
  showMessage(msg, 'warning')
}

export function getParamsFromLists(list, key, val, attr = '') {
  if (!list || list.length === 0) {
    if (attr) {
      return val
    } else {
      return null
    }
  }
  const data = list.filter(r => r[key] === val)
  if (data.length === 0) {
    if (attr) {
      return val
    } else {
      return null
    }
  }
  if (attr) {
    return data[0][attr]
  } else {
    return data[0]
  }
}

export function getNullParamsFromLists(list, key, val, attr = '') {
  if (!list || list.length === 0) {
    if (attr) {
      return val
    } else {
      return null
    }
  }
  const data = list.filter(r => r[key] != val)
  return data
}

export function getMappingValueArrayOfKey(array, keyName) {
  if (Object.prototype.toString.call(array) == '[object Array]') {
    return array.map((item) => {
      return item[keyName]
    })
  }
  return 'null（参数一应为对象数组）';
}

export function getSelectData(array, key1) {
  if (Object.prototype.toString.call(array) == '[object Array]') {
    return array.map((item) => {
      return {
        label: item.id,
        value: item[key1],
      }
    })
  }
  return 'null（参数一应为对象数组）';
}

export function getArryDataFilterItem(array, item) {
  let arryData = array.filter(obj => obj[item] !== null && obj[item] !== '');
  return arryData;
}

function getCurrentDateTime() {
  const now = new Date();
  return now.toISOString().replace(/[-:]/g, '').split('.')[0] + 'Z';
}

export default {
  showMessage,
  showMessageSuccess,
  showMessageError,
  showMessageInfo,
  showMessageWarning,
  getParamsFromLists,
  getNullParamsFromLists,
  getMappingValueArrayOfKey,
  getSelectData,
  getCurrentDateTime,
  getArryDataFilterItem
}